var messageBoxTemplate = '<div class="modal fade message-box-modal" tabindex="-1" role="dialog" aria-labelledby="message-box-title aria-hidden="true">\n' +
  						 '	<div class="modal-dialog@size message-box-modal-dialog">\n' +
    					 '		<div class="modal-content">\n' +
      					 '			<div class="modal-header">\n' +
        				 '				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>\n' +
        				 '				<h4 class="modal-title" id="message-box-title">@title</h4>\n' +
      					 '			</div>\n' +
      					 '			<div class="modal-body">\n' +
        				 '				@message\n' +
      					 '			</div>\n' +
      					 '			<div class="modal-footer">\n' +
        				 '				@buttons\n' +
      					 '			</div>\n' +
    					 '		</div>\n' +
  						 '	</div>\n' +
						 '</div>';

var MessageBox = function() {

	var self = this;

	self.buttons = [];

};

var messageBox = new MessageBox();

var MessageBoxButton = function(caption, handler) {

	var self = this;

	self.caption = caption;

	self.handler = handler;

	self.name = function() {
		return self.caption.replace(' ', '-').replace('"', '\\"').toLowerCase();
	};

	self.dismissButton = function() {
		var dismiss = false;

		if (self.caption != undefined &&
			self.caption != null) {
			var caption = self.caption.toLowerCase().trim();
			dismiss = (caption == 'close' || caption == 'cancel');
		}

		return dismiss;
	};

};

messageBox.show = function(options) {
	var mBox = null;

	messageBox.buttons = [];

	try {
		if (messageBox.buttonSelected != undefined) delete messageBox.buttonSelected;
	}
	catch (ex) {

	}
	
	if (options != undefined &&
		options != null) {
		var title = 'Notification';

		if (options.title != undefined) {
			if (options.title != null) title = options.title;
		}

		var message = '';

		if (options.message != undefined) {
			if (options.message != null) message = options.message;
		}

		var buttons = [];

		if (options.buttons != undefined) {
			if (options.buttons != null) {
				if (options.buttons.length != undefined) {
					for (i = 0; i < options.buttons.length; i++) {
						var button = options.buttons[i];
						if (button.caption != undefined) {
							if (button.caption != null) {
								var caption = button.caption;
								var handler = function() { };

								if (button.handler != undefined &&
									button.handler != null) handler = button.handler;

								buttons.push(new MessageBoxButton(caption, handler)); 
							}
						}
					}
				}
			}
		}

		if (options.buttonSelected != undefined) {
			if (options.buttonSelected != null) messageBox.buttonSelected = options.buttonSelected;
		}

		if (buttons.length <= 0) buttons.push(new MessageBoxButton('Close', function() { }));
		messageBox.buttons = buttons;

		var buttonsHtml = '';

		for (i = 0; i < buttons.length; i++) {
			var button = buttons[i];
			var dismiss = button.dismissButton();
			buttonsHtml += '<button type="button" class="btn btn-' + (dismiss? 'default' : 'primary' ) + ' message-box-button message-box-button-' + button.name() +'"' + (dismiss? ' data-dismiss="modal"' : '') + '>' + button.caption + '</button>\n';
		}

		var size = '';
		
		if (options.size != undefined) {
			if (options.size != null) {
				var assignedSize = options.size.toLowerCase();

				if (assignedSize == 'lg' || assignedSize == 'sm') size = assignedSize;
				else {
					if (assignedSize == 'large') size = 'lg';
					else if (assignedSize == 'small') size = 'sm';
					else { }
				} 
			}
		}

		if (size.trim() != '') size = '  modal-' + size;

		var messageBoxHtml = messageBoxTemplate.replace('@size', size).replace('@title', title).replace('@message', message).replace('@buttons', buttonsHtml);
		mBox = $('.modal.fade.message-box-modal');
		if (mBox.length > 0) mBox.remove();

		$('body').append(messageBoxHtml);
		 mBox = $('.modal.fade.message-box-modal');

		for (i = 0; i < buttons.length; i++) {
			var button = buttons[i];

			if (!button.dismissButton()) {
				var buttonElement = mBox.find('.btn.message-box-button-' + button.name());
				buttonElement.unbind('click');
				buttonElement.click(function(sender) {
					var currentButton = $(this);
					var buttonName = currentButton.text().replace(' ', '-').replace('"', '\\"').toLowerCase();
					var selectedButton = null;

					var buttonHandler = null;

					for (i = 0; i < messageBox.buttons.length; i++) {
						var messageBoxButton = messageBox.buttons[i];
						if (messageBoxButton.name() == buttonName) {
							buttonHandler = messageBoxButton.handler;
							selectedButton = messageBoxButton;
							break;
						}
					}

					if (buttonHandler != null) {
						try {
							buttonHandler();
						}
						catch(ex) {
							try {
								console.error(ex);
							}
							catch (x) {

							}
						}	
					}
					
					var hideDialog = true;

					if (messageBox.buttonSelected != undefined) {
						if (messageBox.buttonSelected != null) {
							try {
								var result = messageBox.buttonSelected( {  'messageBox': mBox, 'button': selectedButton, 'cancel': false, 'sender': currentButton } );
								if (result != undefined &&
									result != null) {
									if (result.cancel != undefined &&
										result.cancel != null) hideDialog = (!result.cancel);
								}
							}
							catch (ex) {

							}
						}
					}

					if (hideDialog) mBox.modal('hide');
				});
			}
		}

		mBox.unbind('hidden.bs.modal');
		mBox.on('hidden.bs.modal', function() {
			mBox.remove();
		});

		mBox.modal('show');
	}

	return mBox;
};